package com.tgky.ybzb.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tgky.ybzb.entity.Jcyb;
import com.tgky.ybzb.service.IJcybService;
import com.tgky.ybzb.utils.TimeConvert;
import com.tgky.ybzb.utils.excel.ExcelUtils;
import org.springframework.web.bind.annotation.*;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import com.tgky.ybzb.entity.Jcybj;
import com.tgky.ybzb.service.IJcybjService;
import com.tgky.ybzb.common.IdParam;
import com.tgky.ybzb.common.PageParam;
import com.tgky.ybzb.common.Result;
import com.tgky.ybzb.utils.MPUtil;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.tgky.ybzb.constant.ResultConstant.ERROR_500;

/**
 * 决策样本集 前端控制器
 *
 * @author XYP
 * @since 2024-07-22
 */
@RestController
@AllArgsConstructor
@Slf4j
@Api(value = "决策样本集", tags = "决策样本集接口")
@RequestMapping("jcybj")
public class JcybjController {

    @Resource
    private IJcybjService jcybjService;

    @Resource
    private IJcybService jcybService;

    @PostMapping("/import")
    @ApiOperation(value = "excel导入")
    public Result excelImport(MultipartFile file,@RequestParam(required = true) String type) {
        try {
            String originalFilename = file.getOriginalFilename();
            String[] jcybjData = originalFilename.substring(0, originalFilename.lastIndexOf(".")).split("-");
            if (jcybjData.length != 3) {
                return Result.error("决策样本集文件名不符合规范！");
            }
            //解析任务案例集合数据
            List<Jcyb> jcybList = ExcelUtils.readMultipartFile(file, Jcyb.class);

            Jcybj jcybj = new Jcybj();
            jcybj.setJcybjID(jcybjData[0]);
            jcybj.setXqcj(jcybjData[1]);
            jcybj.setMbssdlqy(jcybjData[2]);
            jcybj.setYbsl(jcybList.size());
            jcybj.setCreateTime(TimeConvert.getCurrentDateTime());
            //当类型为1则解析文件，为2则保存文件数据
            if(type!=null && !"1".equals(type)){
                //存储决策样本集信息
                jcybjService.save(jcybj);
            }

            if (jcybList != null && jcybList.size() > 0) {
                //存储关联决策样本信息
                jcybList.stream().forEach(jcyb -> {
                    jcyb.setJcybjId(jcybj.getId());
                    jcyb.setCreateTime(TimeConvert.getCurrentDateTime());
                });
                if(type!=null && !"1".equals(type)) {
                    jcybService.saveBatch(jcybList);
                }
            }
            jcybj.setJcybList(jcybList);
            if(type!=null && "1".equals(type)){
                return Result.successData(jcybj);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(ERROR_500, "系统错误");
        }
        return Result.success("决策样本集导入成功！");
    }


    /**
     * 新增
     */
    @PostMapping("/save")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "新增", notes = "传入jcybj")
    public Result save(@RequestBody Jcybj jcybj) {
        if (jcybj == null) {
            return Result.error("参数异常");
        }
        jcybj.setCreateTime(TimeConvert.getCurrentDateTime());
        return Result.status(jcybjService.save(jcybj));
    }

    /**
     * 修改 决策样本集
     */
    @PostMapping("/update")
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "修改", notes = "传入jcybj")
    public Result update(@RequestBody Jcybj jcybj) {
        if (jcybj == null) {
            return Result.error("参数异常");
        }
        return Result.status(jcybjService.updateById(jcybj));
    }

    /**
     * 删除 决策样本集
     */
    @PostMapping("/remove")
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "批量删除", notes = "传入ids")
    public Result remove(@RequestBody IdParam<Long> idParam) {
        if (idParam == null) {
            return Result.error("参数异常！");
        }
        // 删除样本表
        for(Long mid:idParam.getIds()){
            Map<String,Object> map=new HashMap();
            map.put("jcybj_id",mid);
            jcybService.removeByMap(map);
        }
        //删除样本集表
        return Result.status(jcybjService.removeByIds(idParam.getIds()));
    }

    /**
     * 详情
     */
    @GetMapping("/detail")
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "详情", notes = "传入id")
    public Result<Jcybj> detail(@RequestParam Long id) {
        if (id == null) {
            return Result.error("参数异常");
        }
        Jcybj detail = jcybjService.getById(id);
        return Result.successData(detail);
    }

    /**
     * 查询 决策样本集
     */
    @GetMapping("/list")
    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "查询", notes = "传入jcybj")
    public Result list(PageParam pageParam,@RequestBody Jcybj jcybj) {
        List<Jcybj> list = jcybjService.list(jcybjService.getQueryWrapper(pageParam, jcybj));
        return Result.successData(list);
    }

    /**
     * 分页 决策样本集
     */
    @GetMapping("/page")
    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "分页", notes = "传入jcybj")
    public Result<IPage<Jcybj>> page(PageParam pageParam,@RequestParam(value = "xqcj",required = false) String xqcj,
                                     @RequestParam(value = "mbssdlqy",required = false) String mbssdlqy) {
        Jcybj jcybj=new Jcybj();
        jcybj.setXqcj(xqcj);
        jcybj.setMbssdlqy(mbssdlqy);
        IPage<Jcybj> pages=jcybjService.getJcybjListAndJcyb(MPUtil.getPage(pageParam),jcybj);
        return Result.successData(pages);
    }

}
